$(function() {
	flushLoginTicket();
    var orderIntTxnCd = $.trim($("#orderIntTxnCd").val());

	if($.trim($("#loginEmail").val())!=''){
		$("#gopaySubmitBtn")[0].disabled = false;
		$("#gopaySubmitBtn").html("确认支付");
	}else{
		$("#loginSubmitBtn").html("登录");
		$("#loginSubmitBtn")[0].disabled = false;
	}
	
	$("#corpCustLi").click(function() {
		var currentIdx = $(this).parent().find(".cur").index(), selectIdx = $(this).index();
		if (selectIdx == currentIdx) {
			return false;
		}
		$(this).siblings("li").removeClass("cur");
		$(this).addClass("cur");
		$("#operIdTr").show();
		$("#userType").val("20");
		$("#registHref").attr("href",$("#portalUrl").val()+"/cust/register/toRegister.shtml?flag=corp");
	});
	$("#personalCustLi").click(function() {
		var currentIdx = $(this).parent().find(".cur").index(), selectIdx = $(this).index();
		if (selectIdx == currentIdx) {
			return false;
		}
		$(this).siblings("li").removeClass("cur");
		$(this).addClass("cur");
		$("#operIdTr").hide();
		$("#userType").val("10");
		$("#registHref").attr("href",$("#portalUrl").val()+"/cust/register/toRegister.shtml");
	});
	
	var casCustUserType = $("#casCustUserType").val();
	if(casCustUserType){
		if(casCustUserType=="10"){
			$("#corpCustLi").removeClass("cur");
			$("#personalCustLi").addClass("cur");
			$("#operIdTr").hide();
			$("#userType").val("10");
		}else{
			$("#personalCustLi").removeClass("cur");
			$("#corpCustLi").addClass("cur");
			$("#operIdTr").show();
			$("#userType").val("20");
		}
	}

    if(orderIntTxnCd == '01300' || orderIntTxnCd == '01302'){//p2p只针对个人用户
        $("#personalCustLi").trigger("click");
        $("#corpCustLi").hide();
        $("#displayPayPwdTr").hide();
        $("#gopaySubmitBtn").html("下一步");
        $("#gopayConfirmForm").attr("action",ctx+"/sec/pay/gopayChannelConfirm.shtml");

        $("#username").hide();
        $("#userNameTd").append($("#username").val());
    }

    $("#gopaySubmitBtn").click(function() {
        if(orderIntTxnCd == '01300' || orderIntTxnCd == '01302'){
            gopaySubmitNoPwd();
        }else{
            gopaySubmit();
        }
    });
    $("#operType").change(function(){
        var operType = $("#operType").val();
        if(operType == "1"){
            $("#oprId").val("999");
            $("#operSpan").hide();
        }else{
            $("#oprId").val("");
            $("#operSpan").show();
        }
    });
});

function loginSubmit(){
	var msg = "";
	if ($.trim($('#lt').val()).length == 0) {
		msg = "获取认证码失败。";
		flushLoginTicket();
	} else if ($.trim($('#username').val()).length == 0) {
		msg = "用户名不能为空。";
	} else if (($("#userType").val()=="20")) {
        if(($.trim($('#oprId').val()).length == 0)){
            msg = "操作员ID不能为空。";
        }else if($("#operType").val() == "2" && $.trim($('#oprId').val()) =="999") {
            msg = "此ID为管理员ID，请重新选择或填写";
        }
	}else if ($.trim($('#authCode').val()).length == 0) {
		msg = "验证码不能为空。";
	}
	if (msg && msg.length > 0) {
		$('#errorMsg').fadeOut().text(msg).fadeIn();
		return false;
	}
	
	$.get(ctx+"/getPayPwdUUID.shtml",{ts:new Date().getTime(),count:1,safe:0,authCode:$('#authCode').val(),pwdLogin:"1"},function(r){
		if(r){
			if(r.authCodeCheck=="0"){
				var msg2 = "验证码输入错误";
				$('#errorMsg').fadeOut().text(msg2).fadeIn();
				refreshAuthCode();
				return false;
			}else if(r.code1){
				$("#loginSubmitBtn").html("登录中...");
				$("#loginSubmitBtn")[0].disabled = true;
				loginPwdCtrl.encrypt(r.code1,0);
				loginPwdCtrl.finger();
				$("#pwdToken").val(r.code1);
				$("#loginSSOForm").submit();
			}else{
				var msg2 = "获取支付密码随机码失败";
				$('#errorMsg').fadeOut().text(msg2).fadeIn();
				return false;
			}
		}else{
			var msg2 = "获取验证数据失败";
			$('#errorMsg').fadeOut().text(msg2).fadeIn();
			return false;
		}
	},"json");
}


var flushLoginTicket = function() {
	if($("#loginEmail").val()==""){
//		var orderId = $("#orderId").val();
//		var _services = 'service='+ encodeURIComponent(cashier_url+'/sec/pay/info.shtml?ch=06');
        var _services = 'service='+ encodeURIComponent(cashier_url+'/sec/pay/info.shtml?orderKey='+orderKey+'&orderId='+orderId);
		var url = cas_server_url+'/login?' + _services+ '&get-lt=true&n=' + new Date().getTime();
	    var jsonpScript = document.createElement('script');
	    jsonpScript.setAttribute('src', url);
	    document.getElementsByTagName('head')[0].appendChild(jsonpScript);
	}
};


function callback(data){
	$("#lt").val(data.loginTicket);
}

var getAvaAmtByAcctId = function(acctId){
	var url = ctx+"/sec/pay/getAvaAmt.shtml";
	$.get(url,{acctId:$(acctId).val()},function(data){
		$("#avaAmtSpan").html(data.avaAmt+"元");
	},"json");
};


function gopaySubmit(){
	$.get(ctx+"/getPayPwdUUID.shtml",{ts:new Date().getTime(),count:1,safe:1},function(r){
		if(r&&r.code1){
			gopayPwdCtrl.encrypt(r.code1,1);
            gopaySubmitNoPwd();
		}else{
			$.alert("获取支付密码随机码失败");
		}
	},"json");
}

function gopaySubmitNoPwd(){
    $("#gopayConfirmForm").submit();
    $("#gopaySubmitBtn")[0].disabled = true;
    $("#gopaySubmitBtn").html("处理中...");
}

var refreshAuthCode = function(){
	$("#authCode").val("");
    var timestamp = (new Date()).getTime();
    $("#authCodeImg").attr("src", ctx+"/captcha/default.jpeg?timestamp=" + timestamp);
};
